package config.indoor.nexd.com.nexdmapconfig.ui.activity;

import android.content.Intent;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import config.indoor.nexd.com.nexdmapconfig.R;
import config.indoor.nexd.com.nexdmapconfig.presenter.SignInPresenter;
import config.indoor.nexd.com.nexdmapconfig.ui.iView.ISignInActivityView;
import config.indoor.nexd.com.nexdmapconfig.util.ProgressDialogUtils;

/**
 * A login screen that offers login via email/password.
 */
public class SignInActivity extends BaseActivity<SignInPresenter> implements ISignInActivityView {

    private final String TAG = this.getClass().getSimpleName();
    public static final int UPDATE_ALL_CHECKBOX = 0;
    public static final int UPDATE_SAVE_CHECKBOX = 1;
    @Bind(R.id.email)
    AutoCompleteTextView email;

    @Bind(R.id.password)
    EditText password;

    @Bind(R.id.save_password)
    AppCompatCheckBox savePassword;

    @Bind(R.id.auto_signin)
    AppCompatCheckBox autoSignIn;

    @Bind(R.id.signIn)
    Button signIn;


    @Override
    protected int getLayoutResId() {
        Log.d(TAG, "load sign in layout");
        return R.layout.activity_login;
    }

    @Override
    protected void initPresenter() {
        presenter = new SignInPresenter(this, this);
        presenter.init();
    }

    @Override
    public void showToast(String message) {
        Toast.makeText(SignInActivity.this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void initView() {
        presenter.getAPFromShare();
    }

    @Override
    public void setAccountPassword(String account, String password) {
        this.email.setText(account);
        this.password.setText(password);
    }

    @OnFocusChange({R.id.email, R.id.password})
    public void onFocusChangedCheck(View view, boolean hasFocus) {
        if (view.getId() == R.id.email) {
            if (!hasFocus) {
                presenter.checkEmail(email.getText().toString().trim());
            }
        } else if (view.getId() == R.id.password) {
            if (!hasFocus) {
                presenter.checkPassword(password.getText().toString().trim());
            }
        }
    }


    @OnClick(R.id.signIn)
    public void onClick(View view) {
        if (view.getId() == R.id.signIn) {
            email.clearFocus();
            password.requestFocus();
            password.clearFocus();
            signIn.requestFocus();
            // 判断当前是否被点击,点击设置后面可以更新存储账户信息
            if (autoSignIn.isChecked()) {
                Log.d(TAG, "Auto Sign In");
                presenter.isAutoSignIn(true);
            } else if (savePassword.isChecked()) {
                presenter.isSavePassword(true);
            }
            presenter.signIn(email.getText().toString().trim(), password.getText().toString().trim());
        }
    }


    @OnCheckedChanged({R.id.save_password, R.id.auto_signin})
    public void onCheckedChangedCheck(CompoundButton view, boolean isChecked) {
        if (view.getId() == R.id.auto_signin) {
            if (isChecked) {
                savePassword.setChecked(true);
            } else {
                savePassword.setChecked(false);
            }
        }
    }

    @Override
    public void setEmailError(String errorMsg) {
        email.setError(errorMsg);
    }

    @Override
    public void setPasswordError(String errorMsg) {
        password.setError(errorMsg);
    }

    @Override
    public void signInSuccess() {
        startActivity(new Intent(this, ConfigActivity.class));
        finish();
    }

    @Override
    public void showProgress() {
        ProgressDialogUtils.showProgressDialog(this, "Sign In ...", "Sign In", false);
    }

    @Override
    public void hideProgress() {
        ProgressDialogUtils.dismissProgressDialog(this);
    }

    @Override
    public void updateCheckBox(int method) {
        if (method == UPDATE_ALL_CHECKBOX) {
            savePassword.setChecked(true);
            autoSignIn.setChecked(true);
        } else if (method == UPDATE_SAVE_CHECKBOX) {
            savePassword.setChecked(true);
            autoSignIn.setChecked(false);
        }
    }
}

